package com.rub.android.jetpack

import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import com.rub.android.R
import com.rub.android.utils.logI
import kotlinx.android.synthetic.main.fragment_deatil.*

class DetailFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //设置菜单生效隐藏
        setHasOptionsMenu(true)
        return inflater.inflate(R.layout.fragment_deatil, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        /* use requireArguments(),not use arguments!! or getArgument()
         * requireArguments()空会抛出异常，getArgument方法空不会抛出异常，要使用requireArguments方法得结合getArgument以前使用
         * 原因在my_nav_graph的配置里面action_detailFragment_to_homeFragment没有配置argument参数
         */
        if (arguments != null) {
            val args = HomeFragmentArgs.fromBundle(requireArguments())
            logI(TAG, "age=${args.age},userName=${args.userName}")
        }

        button.setOnClickListener {
            var navController = Navigation.findNavController(it)
            navController.navigate(R.id.action_detailFragment_to_homeFragment)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        //隐藏菜单
        menu.clear()
        super.onCreateOptionsMenu(menu, inflater)
    }

    companion object {
        private val TAG = javaClass.simpleName
    }


}